<template>
	<view class="edit">
		<!-- 新增显示项目级别 -->
		<u-cell-item v-if="title == $t('project.btn_add')" class="edit-cell_item" :title-style="titleStyle" bg-color="#ffffff"
			:title="$t('project.prject_level')" :arrow="false">
			<view class="edit-cell_item--input">
				<u-radio-group v-model="form.Level" @change="radioGroupChange">
					<u-radio shape="square" v-for="(item, index) in level_options"
						:key="index" :name="item.name" :disabled="item.disabled">
						{{ item.name }}
					</u-radio>
				</u-radio-group>
			</view>
		</u-cell-item>
		<u-cell-item class="edit-cell_item" :title-style="titleStyle" bg-color="#ffffff" :title="$t('project.prject_code')"
			:arrow="false">
			<view class="edit-cell_item--input">
				<u-input v-model="form.IndexNo" :placeholder="$t('project.pub_tips')" type="text" maxlength="11" :border="false"
					:clearable="false" input-align="right" />
			</view>
		</u-cell-item>
		<u-cell-item class="edit-cell_item edit-cell_item--textarea" :title-style="titleStyle" bg-color="#ffffff"
			:title="$t('project.prject_cnt')" :arrow="false">
			<view class="edit-cell_item--input">
				<u-input v-model="form.ItemName" :placeholder="$t('project.pub_tips')" type="textarea" maxlength="94"
					:border="false" :clearable="false" input-align="right" />
				<text class="ipt-tip">{{$t('project.max_length')}}</text>
			</view>
		</u-cell-item>
		<u-cell-item class="edit-cell_item" :title-style="titleStyle" bg-color="#ffffff" :title="$t('project.unit')"
			:arrow="false">
			<view class="edit-cell_item--input">
				<u-input v-model="form.Unit" type="text" placeholder="" :border="false" :clearable="false"
					input-align="right" />
			</view>
		</u-cell-item>
		<u-cell-item class="edit-cell_item" :title-style="titleStyle" bg-color="#ffffff" :title="$t('project.quantity')"
			:arrow="false">
			<view class="edit-cell_item--input">
				<u-input v-model="form.QtyText" placeholder="" @input="inputChange()"
					:border="false" :clearable="false" input-align="right"/>
			</view>
		</u-cell-item>
		<u-cell-item class="edit-cell_item" :title-style="titleStyle" bg-color="#ffffff" :title="$t('project.price')"
			:arrow="false">
			<view v-if="prcPermission == 1" class="edit-cell_item--input">
				<u-input v-model="form.PriceText" placeholder="" @input="inputChange()"
					:border="false" :clearable="false" input-align="right" :disabled="prcPermissiondo == 1 ? false: true" />
			</view>
		</u-cell-item>
		<u-cell-item class="edit-cell_item" :title-style="titleStyle" bg-color="#ffffff" :title="$t('project.amount')"
			:arrow="false">
			<view v-if="prcPermission == 1" class="edit-cell_item--input">
				<u-input v-model="form.AmountText" disabled placeholder="" :border="false"
					:clearable="false" input-align="right" />
			</view>
		</u-cell-item>
		<u-cell-item class="edit-cell_item" :title-style="titleStyle" bg-color="#ffffff" :title="$t('project.progress_status')"
			:arrow="false" @click="showProgressPicker = true" v-if="this.userType == 1">
			<view class="edit-cell_item--input">
			  <u-picker mode="selector" v-model="showProgressPicker" :default-selector="[0]" :range="progerssSelector" range-key="cateName" @confirm="chooseProgressType"></u-picker>
			  <text style="color: #333">{{ progressText }}</text>
			</view>
		</u-cell-item>
		<u-cell-item class="edit-cell_item" :title-style="titleStyle" bg-color="#ffffff" :title="$t('project.add_sub_type')"
			:arrow="false" @click="showChangePicker = true" v-if="this.userType == 1">
			<view class="edit-cell_item--input">
				<u-picker mode="selector" v-model="showChangePicker" :default-selector="[0]" :range="changeSelector" range-key="cateName" @confirm="chooseChangeType"></u-picker>
			  <text style="color: #333">{{ changeText }}</text>
			</view>
		</u-cell-item>
		<u-cell-item class="edit-cell_item edit-cell_item--textarea" :title-style="titleStyle" bg-color="#ffffff"
			:title="$t('project.remark')" :arrow="false">
			<view class="edit-cell_item--input">
				<u-input v-model="form.Rmk" :placeholder="$t('project.pub_tips')" type="textarea" maxlength="108" :border="false"
					:clearable="false" input-align="right" />
				<text class="ipt-tip">{{$t('project.max_length')}}</text>
			</view>
		</u-cell-item>
		<u-cell-item v-if="this.userType == 1 && isNotifyClient == 0" class="edit-cell_item flex" :title-style="titleStyle" bg-color="#ffffff" title="是否通知顾客确认" :arrow="false">
		  <u-switch v-model="confirmValue" @change="switchChange" style="margin-top: 10rpx" ></u-switch>
		</u-cell-item>
		<u-cell-item v-if="this.userType == 1 && confirmValue === true && apyAccPermission == 1" class="info-cell_item" :title-style="titleStyle" bg-color="#ffffff" title="" :value="clientValue"
			@click="isCheckPopup=true">
		</u-cell-item>
<!-- 		<u-cell-item v-if="this.vuex_user.type == 1 && confirmValue === true && projectInfo.SelfIsBidPriceAcc == 1" class="info-cell_item" :title-style="titleStyle" bg-color="#ffffff" title="" :value="clientValue"
			@click="isCheckPopup=true">
		</u-cell-item> -->
		<view class="edit-button">
			<u-button v-if="this.stat!=5 && this.stat!=6" type="primary" @click="submit">{{$t('project.btn_save')}}</u-button>
			<u-button v-if="this.stat==6" type="primary" @click="submit">{{$t('project.client_rej_edit_save')}}</u-button>
			<u-button v-if="this.stat==5" type="primary" @click="submit">{{$t('project.emp_rej_edit_save')}}</u-button>
		</view>
		<!-- 消息提示 -->
		<u-toast ref="uToast" />

		<!-- 下拉选择器 -->
		<u-popup v-model="isCheckPopup" mode="center" border-radius="20"  v-if="this.userType == 1 && confirmValue === true">
			<view class="check-box">
				<view class="check-title">选择通知客户</view>
				<scroll-view scroll-y="true" style="height: 500rpx;">
					<u-radio-group @change="radioChange">
						<view class="check-item u-flex u-border-bottom"
							v-for="(item, index) in clientList" :key="index">
							<view class="check-item_txt u-flex-1">{{item.nickname}}</view>
							<u-radio :name="item.user" v-model="item.user"></u-radio>
						</view>
					</u-radio-group>
				</scroll-view>
				<view class="confrim-btn">
					<u-button type="primary" @click="nextFunc">确认</u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
import pageMixin from '@/utils/pageMixin';
import store from '@/store/index.js';
export default {
  mixins: [pageMixin],
		data() {
			return {
				userType: 2,
				title: '',
				project: 0,
				projectInfo: {},
				iid: 0,
				form: {
					Level: '同级',
					Qty: 0,
					PriceText: '0',
					ChangeType: 1,
				},
				tempForm: {
					Level: '同级',
					Qty: 0,
					PriceText: '0',
				},
				pcode: '',
				cnt: '',
				unit: '',
				num: '',
				price: '',
				total: '',
				tip: '',
				titleStyle: {
					color: '#131313',
					fontSize: '32rpx'
				},
				list: [{
						name: '同级',
						disabled: false,
					},
					{
						name: '子项',
						disabled: false
					}
				],
				value: '同级',
				checked: false,

				clientUid: 0,
				clientList: [],
				clientValue: '请选择客户',
				isCheckPopup: false,
				prcPermission: 1,
				prcPermissiondo: 1,
				confirmValue: false,
				stat: 0,
				isNotifyClient: 0,
				showProgressPicker: false,
				showChangePicker: false,
				progressText: '待勘验',
				changeText: '原始工程',
				progerssSelector: [
			    {
						cateName: this.$t('project.progress_stat[1]'),
						id: 1
					},
					{
						cateName: this.$t('project.progress_stat[2]'),
						id: 2
					},
					{
						cateName: this.$t('project.progress_stat[3]'),
						id: 3
					},
					{
						cateName: this.$t('project.progress_stat[4]'),
						id: 4
					},
					{
						cateName: this.$t('project.progress_stat[5]'),
						id: 5
					}
				],
				changeSelector: [
					{
						cateName: this.$t('project.add_sub_pro[1]'),
						id: 1
					},
					{
						cateName: this.$t('project.add_sub_pro[2]'),
						id: 2
					},
					{
						cateName: this.$t('project.add_sub_pro[3]'),
						id: 3
					},
					{
						cateName: this.$t('project.add_sub_pro[4]'),
						id: 4
					},
					{
						cateName: this.$t('project.add_sub_pro[5]'),
						id: 5
					}
				],
				apyAccPermission:0,
			}
		},
		destroyed() {
			this.$http.post('/mgr/biz/project/item/save/unlock', {
				 iid: this.iid,
				 pid: this.project,
				 }).then(res=>{
					 if(res=="success"){
						console.log("撤销锁成功");
					 }
				 })

		},
		onLoad(opt) {
			this.userType = store.state.vuex_user.type || 2
			this.title = opt.title;
			this.project = opt.project;
			this.iid = opt.iid;
			this.stat= opt.stat || 0;
			if(uni.getStorageSync('type') == 2){
				this.changeText = '追加工程'
				this.form.ChangeType = 2
			}else{
				this.form.ChangeType = 1
			}
			this.getInfo();
			this.getProjectClient();
			this.getPermissionPrice();
			if(this.userType == 1){
				this.isSendClient();
				this.getPermissionPriceDo();
				this.getProjectInfo();
				this.getPermissionPriceAcc();
			}
			if(this.title == '编辑' || this.title == 'Edit' || this.title == 'Редактировать') {
				uni.setNavigationBarTitle({
					title: this.$t('project.btn_edit_project')
				})
			}else {
				uni.setNavigationBarTitle({
					title: this.$t('project.btn_add_project')
				})
			}
		},
		onShow() {},
		components: {},
		computed: {
			level_options() {
				return [
					{
							name: this.$t('project.level_options[0].name'),
							disabled: false,
						},
						{
							name: this.$t('project.level_options[1].name'),
							disabled: false
						}
				]
			}
		},
		methods: {
			getProjectInfo() {
				if (this.project > 0) {
					this.$http.get('/mgr/biz/project/info', {pid: this.project}).then(res => {
						this.projectInfo = res;
					})
				}
			},
			//项目出价人权限
			getPermissionBidPriceAcc() {
				this.$http.get('/client/biz/project/item/permission/BidPriceAcc', {
					project: this.project
				}).then(res => {
					this.apyAccPermission = res;
				});
			},
			//出价权限
			getPermissionPriceAcc() {
				this.$http.get('/client/biz/project/item/permission/PriceAcc', {
					// project: this.project
				}).then(res => {
					// this.apyPermission = res;
					this.apyAccPermission = res;
				});
			},
			getInfo() {
				if ((this.project > 0 || this.iid > 0) && (this.title == '编辑' || this.title == 'Edit' || this.title ==
						'Редактировать')) {
					this.$http.get('/client/biz/project/item/info', {
						'project': this.project,
						'iid': this.iid
					}).then(res => {
						this.form = res
						this.tempForm = res
						this.progressText = this.$t('project.progress_stat.'+res.ProcessStat)
						this.changeText = this.$t('project.add_sub_pro.'+res.ChangeType)
					})
				}
			},
			isSendClient(){
				this.$http.get('/client/biz/project/item/permission/apy',{'project':this.project}).then(res=>{
					this.isNotifyClient = res
				})
			},
			chooseProgressType(value) {
				this.form.ProcessStat = value[0] + 1
				this.progressText = this.progerssSelector[this.form.ProcessStat - 1].cateName
			},
			chooseChangeType(value) {
				this.form.ChangeType = value[0] + 1
				this.changeText = this.changeSelector[this.form.ChangeType - 1].cateName
			},
			switchChange(value){
				if(value === false){
					this.clientUid = 0
				}
			},
			radioGroupChange(e){
				this.form.Level = e
			},
			getProjectClient() {
				this.$http.get('/client/biz/person/list', {
					project: this.project,
					user_type: 2,
					is_message: 1
				}).then(res => {
					this.clientList = res;
				});
			},
			radioChange(e) {
				this.clientUid = e;
			},
			nextFunc() {
				let that = this;
				let clientUid = that.clientUid || 0;
				if (clientUid == 0) {
					uni.showToast({
						title: '请选择通知客户',
						icon: 'none',
						duration: 2000
					});
					return
				}
				for (let i=0; i < that.clientList.length; i++) {
					if (that.clientList[i].user == clientUid) {
						that.clientValue = that.clientList[i].nickname;
						break;
					}
				}

				that.isCheckPopup = false;
			},
			inputChange(e) {
				console.log(e)
				if(this.form.QtyText == ''||this.form.QtyText == undefined){
					this.form.QtyText = 0
				}
				if(this.form.PriceText == ''|| this.form.PriceText == undefined){
					this.form.PriceText = '0'
				}
				//如不加延时操作,此处priceText会取到数值改变之前的值
				setTimeout(()=> {
					let qty = 0
					let price = 0
					var idx = this.form.QtyText.indexOf(".")
					var priceIdx = this.form.PriceText.indexOf(".")
					if(idx == this.form.QtyText.length-1 || priceIdx == this.form.PriceText.length-1 ){
						console.log("是不是进了这里面")
						qty = this.form.QtyText.split('.')[0]
						price = this.form.PriceText.split('.')[0]
					}else{
						let str = this.form.QtyText.split('.')
						str[0] = str[0].replace(/,/g,'')
						let re = /\d{1,3}(?=(\d{3})+$)/g
						let le = str[0].replace(re, "$&,");
						this.form.QtyText = str.length > 1 && str[1] ? `${le}.${str[1]}` : `${le}.00`
						this.form.QtyText = this.form.QtyText.replace('.00','')
						let qtyStr = this.form.PriceText.split('.')
						qtyStr[0] = qtyStr[0].replace(/,/g,'')
						let qtyRe = /\d{1,3}(?=(\d{3})+$)/g
						let qtyLe = qtyStr[0].replace(re, "$&,")
						this.form.PriceText= qtyStr.length > 1 && qtyStr[1] ? `${qtyLe}.${qtyStr[1]}` : `${qtyLe}.00`
						this.form.PriceText = this.form.PriceText.replace('.00','')
					}
					 qty = this.form.QtyText.replace(/,/g,'')
					 price = this.form.PriceText.replace(/,/g,'')
					 console.log("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"+ parseFloat(qty * price),price,qty)
					 let amount = parseFloat(qty * price)
					// console.log('this.form.QtyText' + this.form.QtyText)
					// console.log('this.form.PriceText', + this.form.PriceText)
					// console.log('this.form.QtyText * this.form.PriceText',this.form.QtyText * this.form.PriceText)
					// console.log('parseFloat(this.form.QtyText * this.form.PriceText)',parseFloat(this.form.QtyText * this.form.PriceText))
					 this.form.AmountText = amount.toFixed(2);
				},100)
				
				
			},
			submit() {
				console.log("----Permission1-----"+this.form.Permission);
				let clientUid = this.clientUid || 0;
				this.form.Type = 1;
				this.form.Qty = parseFloat(this.form.QtyText.replace(/,/g,''));
				if(this.confirmValue === true){
					if (clientUid == 0 && this.clientList.length > 0 && this.userType == 1) {
						uni.showToast({
							title: '请选择通知客户',
							icon: 'none',
							duration: 2000
						});
						return
					}
				}
				if (this.project > 0) {
					this.form.Project = parseInt(this.project);
				}
				if (this.iid > 0) {
					this.form.Iid = parseInt(this.iid);
				}
				if(this.stat>0){
					this.form.IsConfirm = parseInt(this.stat);
				}
				if (this.title == '编辑' || this.title == 'Edit' || this.title == 'Редактировать') {
					this.form.Type = 2;
					this.form.Level = '';
					if (this.form.Permission == 1) {
						// this.form.AmountText = this.form.Amount / 100 + '';
						// this.form.PriceText = this.form.Price / 100 + '';
					}
					else {
						this.form.AmountText = this.tempForm.Amount / 100 + '';
						this.form.PriceText = this.tempForm.Price / 100 + '';
					}
				}else {
					if(this.form.Permission == 0) {
						this.form.AmountText = this.form.Amount / 100 + '';
						this.form.PriceText = this.form.Price / 100 + '';
					}
				}

				this.form.ClientUid = clientUid;
				if (this.form.ClientUid > 0) {
					this.form.IsMsg = true;
				}
				this.$http.post('/client/biz/project/item/save', this.form, store.state.jHeader).then(res => {
					this.title = this.title + this.$t('project.succeeded')
					if (this.form.IsMsg) {
						this.title = this.title + '\r\n' + this.$t('project.WaitingCustomerConfirmation');
					}
				 if (this.userType == 2) {
					this.title = this.title + '\r\n' + this.$t('project.WaitingConfirmation');
				 }
					this.$refs.uToast.show({
						title: this.title,
						icon: false,
					});
					setTimeout(()=> {
						uni.$emit('refreshList')
						uni.navigateBack(1)
					},1000)
				
				});
			},
			//价格可见权限
			getPermissionPrice() {
				this.$http.get('/client/biz/project/item/permission/price', {
					project: this.project
				}).then(res => {
					this.prcPermission = res;
				});
			},
			//报价单权限
			getPermissionPriceDo() {
				this.$http.get('/client/biz/project/item/permission/prices').then(res => {
					this.prcPermissiondo = res;
				});
			},
			change(status) {
				// console.log(status);
			},
			chooseCall: function () {

			},
		}
	}
</script>

<style lang="scss" scoped>
	.edit {
		min-height: 1600rpx;
	}

	.edit-cell_item {
		height: 100rpx;
		background-color: #ffffff;

		&.avatar {
			height: 132rpx;
			// padding: 10rpx 0;
		}

		&--text {
			color: #131313;
		}

		&--avatar {
			position: relative;
			width: 122rpx;
			height: 122rpx;
			margin-left: auto;
			padding: 10rpx;

			.u-upload {
				z-index: 999;
				position: absolute;
				top: 0;
				left: 0;
			}

			.u-list-item {
				margin: 0 ;
				opacity: 0;
			}

		}

		&--label {
			color: $u-type-primary;
			margin-left: 20rpx;

			.icon {
				font-size: 26rpx;
			}
		}

		&--input {
			margin-left: auto;
		}

		.ipt-tip {
			position: absolute;
			bottom: 10rpx;
			right: 30rpx;
			color: #909399;
			font-size: 26rpx;
		}
	}

	.edit-cell_item--input {
		margin-left: 20px;
		width: inherit;
	}

	.edit-cell_item--textarea {
		position: relative;
		height: 298rpx;
		align-items: flex-start;
		overflow: hidden;
	}

	.uni-textarea-wrapper {
		height: 120rpx ;
		overflow: hidden ;
	}

	.edit-button {
		position: fixed;
		width: 100%;
		bottom: 0;
		padding: 20rpx 80rpx;
		z-index: 10000;
		background-color: #FFFFFF;

	}

	.info-cell_item {
		height: 100rpx;
		background-color: #ffffff;
		&.avatar {
			height: 132rpx;
		}

		&--text {
			color: #131313;
		}

		&--avatar {
			position: relative;
			width: 122rpx;
			height: 122rpx;
			margin-left: auto;
			padding: 10rpx;

			.u-upload {
				z-index: 999;
				position: absolute;
				top: 0;
				left: 0;
			}

			.u-list-item {
				margin: 0 ;
				opacity: 0;
			}
		}

		&--input {
			position: relative;
			width: 60%;
			margin-left: auto;
		}
	}


	.check-box {
		width: 600rpx;
		padding: 40rpx;
	}

	.check-title {
		font-size: 32rpx;
		margin-bottom: 20rpx;
	}

	.check-item {
		width: 100%;
		background-color: #fff;
		height: 106rpx;
		padding: 0 24rpx 0 18rpx;
	}
	.flex {
		display: flex;
		align-items: center;
	}
</style>
